package com.example.protestgps;

import org.json.JSONArray;
import org.json.JSONObject;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.os.CountDownTimer;
import android.app.AlertDialog;

public class CountDown extends FragmentActivity{
	
	Button btncount;
	GoogleMap myMap;
	double latchange;
	double lngchange;
	LocationManager lm;
	Context con;
	LatLng start2;
	int click = 0;
	double deslat1=0;
	double deslng1=0 ;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.countdown);
		setwidget();
		lm = (LocationManager) getSystemService(LOCATION_SERVICE);
		con = new CountDown();
		SupportMapFragment mySupport = (SupportMapFragment) getSupportFragmentManager()
				.findFragmentById(R.id.map_canvas);
		myMap = mySupport.getMap();
		myMap.setMyLocationEnabled(true);
		// myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
		Log.d("test myMap", " " + myMap);
		GetLocation local1 = new GetLocation(lm, this, myMap);
		latchange = local1.GetLat();
		lngchange = local1.GetLng();
	    start2 = new LatLng(latchange, lngchange);
		System.out.println(""+latchange);
		CallHTTP(latchange,lngchange);
		final MyCount counter = new MyCount(50000,1000);
		counter.start();
		  btncount.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
				    if(click ==1){
					AlertDialog alertDialog = new AlertDialog.Builder(CountDown.this).create();
					alertDialog.setTitle("Reset...");
					alertDialog.setMessage("Are you sure?");
					alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
					// here you can add functions
						counter.cancel();
					/*	Intent gotonext = new Intent(CountDown.this,NotNullUserID.class);
						startActivity(gotonext);		*/
				   
					 }
					});
					alertDialog.setIcon(R.drawable.ic_launcher);
					alertDialog.show();
			   }
			  }
			});
		
		
	}

	
	public class MyCount extends CountDownTimer{
		public MyCount(long millisInFuture, long countDownInterval) {
		super(millisInFuture, countDownInterval);
		}
		@Override
		public void onFinish() {	
		     
			click = 2;
			if(click == 2){
			postersensor(latchange,lngchange);
			btncount.setText("Send Data!");
			}
		}
	
		@Override
		public void onTick(long millisUntilFinished) {
	    btncount.setText("Left:"+millisUntilFinished/1000);
	    
	    click = 1;

		}
	}
	
	
	
	private void setwidget() {
		// TODO Auto-generated method stub
		
		btncount = (Button) findViewById(R.id.btncoudow);
		
	}


	 public void CallHTTP(double curlat,double curlng) {
		 AsyncTask<String, String, String> AsyncTask1;
			JSONArray JsonArr;
			JSONObject JSONOBJ;
			LatLng despoint;
			PostSvnEmer task1 = new PostSvnEmer();
			// task.setdata();
			task1.curpointemer(curlat, curlng,myMap);
			AsyncTask1 = task1.postter1
					.execute("http://192.168.1.101/myexample/mytest1.php");
			
		 }
	
	 
	 public void recdata1(double _lat,double _lng,double curlat,double curlng,GoogleMap Map){
			Log.d("_lat",""+_lat);
			LatLng curpoint = new LatLng(curlat, curlng);
			LatLng despoint= new LatLng(_lat, _lng);
			Log.d("despoint",""+despoint);
			Log.d("curpoint",""+curpoint);
			Log.d("Map",""+Map);
			Router routerr1 = new Router(Map, curpoint, despoint);
			
		}

	 
	 public void postersensor(double curlat,double curlng) {
			AsyncTask<String, String, String>  AsyncTask1;
			JSONObject JSONOBJ;
			LatLng despoint;
			HttpPosterSensor postsensor = new HttpPosterSensor();
			postsensor.curpoint(curlat, curlng,deslat1,deslng1);
			AsyncTask1 = postsensor.posts.execute("http://192.168.1.101/myexample/emersensorpost.php");
 }
	 
	 
}
